package com.qersoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qersoft.entity.DictionaryType;
import com.qersoft.service.DictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/training/dictionarytype")
public class DictionaryTypeController {

    @Autowired
    private DictionaryTypeService dictionaryTypeService;

    // 新增字典类型
    @PostMapping("add")
    public String add(@RequestBody DictionaryType dictionaryType) {
        if (dictionaryTypeService.add(dictionaryType) > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 根据 ID 删除字典类型
    @DeleteMapping("delete/{id}")
    public String delete(@PathVariable Integer id) {
        if (dictionaryTypeService.delete(id)) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 更新字典类型
    @PutMapping("update")
    public String update(@RequestBody DictionaryType dictionaryType) {
        if (dictionaryTypeService.update(dictionaryType)) {
            return "success";
        } else {
            return "fail";
        }
    }

    // 根据 ID 获取字典类型
    @GetMapping("get/{id}")
    public DictionaryType getById(@PathVariable Integer id) {
        return dictionaryTypeService.getById(id);
    }

    // 查询所有字典类型（带分页）
    @GetMapping("list")
    public IPage<DictionaryType> list(@RequestParam(required = false) String dicName,
                                      @RequestParam(required = false) String dicType,
                                      @RequestParam(defaultValue = "1") Integer pageNo,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        DictionaryType dictionaryType = new DictionaryType();
        dictionaryType.setDicName(dicName);
        dictionaryType.setDicType(dicType);
        return dictionaryTypeService.list(dictionaryType, pageNo, pageSize);
    }
}
